package edu.msg.jthr.backend.repository.bean;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import edu.msg.jthr.backend.model.User;
import edu.msg.jthr.backend.repository.service.UserRepository;

@Stateless(name = "UserRepoBean", mappedName = "ejb/UserRepository")
public class UserRepoBean extends BaseRepoBean<User, Long> implements
		UserRepository {

	@PersistenceContext(unitName = "titan")
	private EntityManager entityManager;
	public Class<User> clazz;
	private static final Logger LOG = LoggerFactory
			.getLogger(UserRepoBean.class);

	public UserRepoBean() {
		super(User.class);
	}

	public User getUserByUserName(String userName) {
		User result = new User();
		try {
			@SuppressWarnings("unchecked")
			TypedQuery<User> query = (TypedQuery<User>) getEntityManager().createQuery("SELECT u FROM User u WHERE u.userName = :name");
			query.setParameter("name", userName);
			result = query.getSingleResult();
		} catch (NoResultException e) {
			LOG.error("Getting user by username failure!", e);
		}
		return result;
	}

}
